﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using EuphoricNation.Util;

namespace EuphoricNation.Rewrite
{
    /// <summary>
    /// Summary description for Routes
    /// </summary>
    public sealed class Routes
    {
        #region Singleton pattern
        static readonly Routes _instance = new Routes();

        static Routes()
        {
        }

	    public Routes()
	    {
	    }

        public static Routes Current
        {
            get
            {
                return _instance;
            }
        }
        #endregion

        private Dictionary<string, Route> _routes = new Dictionary<string, Route>();

        /// <summary>
        /// Routes registered with the site
        /// </summary>
        public Dictionary<string, Route> RouteCollection
        {
            get { return _routes; }
        }

        /// <summary>
        /// Add a new route to the manager
        /// </summary>
        /// <param name="route">Route to add</param>
        public void AddRoute(Route route)
        {
            _routes.Add(route.Path, route);
        }

        /// <summary>
        /// Get the route for a specific path
        /// </summary>
        /// <param name="path">Path</param>
        /// <returns>Matching route</returns>
        public Route GetRoute(string path)
        {
            path = path.Replace(".aspx", "");
            string[] parts = path.Split('/').TrimBlank();

            if (parts.Length == 0)
                return null;

            // Check if we're at a default page, or specific page
            if (parts[parts.Length - 1] == "Default")
            {
                string fullPath = "/" + parts.Concatenate('/') + "/";
                fullPath = fullPath.Replace("Default/", "");

                if (_routes.ContainsKey(fullPath))
                    return _routes[fullPath];
                else
                    return FindRoute(fullPath.Split('/').TrimBlank()); // No 1:1 mapping, try and find a variable match
            }
            else
            {
                string fullPath = parts.Concatenate('/');
                if (_routes.ContainsKey(fullPath))
                    return _routes[fullPath];
                else
                    return FindRoute(parts); // No 1:1 mapping, try and find a variable match
            }
        }

        /// <summary>
        /// Find a matching route
        /// </summary>
        /// <param name="pathParts">The path broken apart by forward slashes</param>
        /// <returns>Matching route (if any). NULL if not found</returns>
        private Route FindRoute(string[] pathParts)
        {
            int temp;
            int currentMax = 0;
            Route currentRoute = null;
            Route tempRoute;

            foreach (KeyValuePair<string, Route> routeMatch in _routes)
            {
                tempRoute = routeMatch.Value;
                temp = tempRoute.MatchesPath(ref pathParts);

                if (temp > currentMax)
                {
                    currentMax = temp;
                    currentRoute = tempRoute;

                    // If this is a perfect match - break out now
                    if (temp == pathParts.Length)
                        break;
                }
            }

            return currentRoute;
        }
    }
}